"use strict";

Components.utils.import("chrome://viewsosource/locale/utility.jsm")
Components.utils.import("resource://viewsosource/modules/utility.jsm")

 if ("undefined" == typeof(viewSoso)) {
    var viewSoso = { }
 }

viewSoso.gJSObj = null;
viewSoso.prefix = "data:application/json;charset=utf-8,"; // only url

viewSoso.checkHighlightSyntax = function(anevt)
{
    let theval = Services.prefs.getBoolPref("view_source.syntax_highlight");
    document.getElementById("menu_highlightSyntax").setAttribute("checked", theval);

    let theval = true;
    if(gPageLoader)
    try {
        if(gPageLoader.currentDescriptor) theval = false;
    }
    catch(err)  {    }

    if(theval)
        document.getElementById("cmd_highlightSyntax").setAttribute("disabled", true)
    else document.getElementById("cmd_highlightSyntax").removeAttribute("disabled");
}

viewSoso.onLoadViewLocalStorage = function()
{
    gBrowser.addEventListener("DOMContentLoaded", viewSoso.checkHighlightSyntax, false);
    if(window.arguments)
    if(window.arguments[3] != 'json')
    {
        //  select or mathxml
        onLoadViewPartialSource();
        return;
    }
//    function censor(key, value) {
//        if (typeof(value) == "string")
    gWrapLongLines = Services.prefs.getBoolPref("view_source.wrap_long_lines");
    document.getElementById("menu_wrapLongLines").setAttribute("checked", gWrapLongLines);

  // all our content is held by the data:URI and URIs are internally stored as utf-8 (see nsIURI.idl)
    const loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
    viewSoso.gJSObj = window.arguments[2];

    gBrowser.addEventListener("DOMContentLoaded", function(anevt)
            {
                let theval = (window.arguments) ? window.arguments[0] : "about:blank";
                gBrowser.contentDocument.title = moduleViewSoso.title + theval;
            }, false);

    var thecontent = JSON.stringify(viewSoso.gJSObj, null, " ");
    //      encodeURI(thecontent);  window.escape(
        thecontent = viewSoso.prefix + encodeURIComponent(thecontent);
    getWebNavigation().loadURI(thecontent, loadFlags, null, null, null);

    gBrowser.droppedLinkHandler = function (event, url, name)   // ? what is it ?
    {
        viewSource(url)
        event.preventDefault();
    }

    window.content.focus();

    return;
}

viewSoso.cmdSavePage = function()
{
const kSaveAsType_URL      = 1; // Save document or URL by itself.
//    alert(window.content.location.href)
    var thehref = window.content.location.href.substr(0, 33);
    if((viewSoso.gJSObj) && (thehref.indexOf(viewSoso.prefix) === 0))
    {
        var aContentType = "application/json";
        var thesaveMode  = GetSaveModeForContentType(aContentType, document);
        var thecharset = document.characterSet;
        let thetitle = window.arguments[0] || "";
            thetitle = thetitle.replace(/^.*:/, ""); // trim schema
            thetitle = thetitle.replace(/(^(\\|\/)+)|((\\|\/)+$)/g, ""); // trim any slash

        var fileInfo = new FileInfo(null, thetitle, null, "json");
//            initFileInfo(fileInfo, null, thecharset, null, aContentType, null);
        var thefile;
        var fpParams = {
            fpTitleKey:   null,
            fileInfo  :   fileInfo,
            contentType:  aContentType,
            saveMode  :   thesaveMode,
            saveAsType:   kSaveAsType_URL,
            file: thefile
        };

// If the method returned false this is because the user cancelled from the save file picker dialog.
        if (getTargetFile(fpParams))
        {
    //          let saveAsType = fpParams.saveAsType;
            thefile = fpParams.file;
            var thecontent = JSON.stringify(viewSoso.gJSObj, null, " ");
            utilityViewSoso.element2file(thecontent, thefile, thecharset);
        }
    }
    else
        internalSave(window.content.location.href.substring(12), 
                    null, null, null, null, null,
                    "SaveLinkTitle", null, null, null,
                    gPageLoader);
}
